package com.mod.enemies {
	import com.calefk.core.EntitySimple;
	import flash.display.MovieClip;
	

	public class Movement {
		
		public var onEndMovement:Function;
		private var mEntity:EntitySimple;
		
		private var mSpeed:Number;
		private var mStartX:Number;
		private var mStartY:Number;
		private var _mEndX:Number;
		private var _mEndY:Number;
		
		private var mTimer:Number = 0;
		private var mTotalTime:Number;
		
		private var mVx:Number;
		private var mVy:Number;
		
		public function Movement(entity:EntitySimple , startX:Number , startY:Number, endX:Number , endY:Number , totalTime:Number) {
			mEntity = entity;
			mStartX = startX;
			mStartY = startY;
			mEndX = endX;
			mEndY = endY;
			
			var dx:Number = mEndX - mStartX;
			var dy:Number = mEndY - mStartY;
			var dst:Number = Math.sqrt(dx * dx + dy * dy);
			
			
			mTotalTime = totalTime;
			
			mSpeed = dst / mTotalTime;
			
			mVx = dx / dst * mSpeed;
			mVy = dy / dst * mSpeed;		
		}
		
		public function update(dt:int):void {
			// update linear movement
			mTimer += (dt / 1000.0);
			mEntity.x = mStartX + (mVx * mTimer);
			mEntity.y = mStartY + (mVy * mTimer);
			
			if (mTimer >= mTotalTime) {
				
				mEntity.x = mEndX;
				mEntity.y = mEndY;
				
				if (onEndMovement != null) {
					onEndMovement();
				}
			}
		}
		
		public function get mEndX():Number 
		{
			return _mEndX;
		}
		
		public function set mEndX(value:Number):void 
		{
			_mEndX = value;
		}
		
		public function get mEndY():Number 
		{
			return _mEndY;
		}
		
		public function set mEndY(value:Number):void 
		{
			_mEndY = value;
		}
	}

}